﻿function CanReorderItems()
{
	var rootFolder = getQueryParam("RootFolder");
	if (rootFolder == null || rootFolder == "")
		return false;

	return true;
}

function moveStep(siteUrl, listTitle, index, elementsToDisable, moveUp)
{
	statusID = SP.UI.Notify.addNotification(L_Loading_Text, true);

	index--;
	elementsToDisableCurrent = elementsToDisable;
	EnableElements(elementsToDisableCurrent, false);

	var ID1 = itemIds[index];
	var ID2;
	if (moveUp)
	{
		ID2 = itemIds[index - 1];
	}
	else
	{
		ID2 = itemIds[index + 1];
	}

	getItems(siteUrl, listTitle, ID1, ID2);
}

var workflowStepOrderField = "WorkflowStepOrder";
function getItems(siteUrl, listTitle, ID1, ID2)
{
	if (siteUrl == null || siteUrl == "")
	{
		siteUrl = "/";
	}

	this.clientContext = new SP.ClientContext(siteUrl);
	this.oList = clientContext.get_web().get_lists().getByTitle(listTitle);

	this.oListItem1 = oList.getItemById(ID1);
	this.oListItem2 = oList.getItemById(ID2);

	clientContext.load(oListItem1, workflowStepOrderField);
	clientContext.load(oListItem2, workflowStepOrderField);

	clientContext.executeQueryAsync(Function.createDelegate(this, this.updateItems), Function.createDelegate(this, this.onQueryFailed));
}

function updateItems()
{
	var orderItem1 = this.oListItem1.get_item(workflowStepOrderField);
	var orderItem2 = this.oListItem2.get_item(workflowStepOrderField);

	this.oListItem1.set_item(workflowStepOrderField, orderItem2);
	this.oListItem2.set_item(workflowStepOrderField, orderItem1);
	
	this.oListItem1.update();
	this.oListItem2.update();

	clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryUpdateItemsSucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQueryUpdateItemsSucceeded()
{
	SP.UI.Notify.removeNotification(statusID);
	statusID = "";

	RefreshPage(SP.UI.DialogResult.OK)
}
